
package com.idealighter.game.games.fqzs.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.core.constant.room.RoomActiveConstant;
import com.idealighter.game.dictionary.dic.GamesDic;
import com.idealighter.game.dictionary.dic.SharkRoomDic;
import com.idealighter.game.dictionary.domain.GamesDomain;
import com.idealighter.game.dictionary.domain.SharkRoomDomain;
import com.idealighter.game.games.common.InstanceState;
import com.idealighter.game.games.fqzs.struct.SharkRoom;
import com.idealighter.game.games.fqzs.struct.SharkSeat;
import com.idealighter.game.games.fqzs.struct.SharkTable;
import com.idealighter.utils.check.EmptyUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 飞禽走兽数据管理.
 * 
 * @date 2016年2月17日 下午12:58:05
 *
 */
@Singleton
public class SharkDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(SharkDataMgr.class);

  // 玩家的座位
  private final Map<Long, SharkSeat> playerSeats = new ConcurrentHashMap<Long, SharkSeat>();

  private final Map<Integer, SharkBaseRoom> roomMap = new HashMap<>();

  private final SharkRoomDic roomDic;

  private final GamesDic gamesDic;

  /**
   * 构造函数.
   * 
   * @param roomDic .
   * @param gamesDic .
   */
  @Inject
  public SharkDataMgr(SharkRoomDic roomDic, GamesDic gamesDic) {
    this.roomDic = roomDic;
    this.gamesDic = gamesDic;
    createRooms(this.roomDic);
  }

  /**
   * 创建房间 .
   *
   * @author abin
   * @date 2018年4月28日 下午2:10:42
   * @param roomDomain 房间dic信息.
   */
  private void createRoom(SharkRoomDomain roomDom) {
    if (roomDom != null && roomDom.getIsActive() == RoomActiveConstant.ACTIVE) {
      GamesDomain gamesDom = gamesDic.get(Game.FQZS.getType());
      if (gamesDom != null && gamesDom.getActive()) {
        this.roomMap.put(roomDom.getId(), new SharkBaseRoom(roomDom));
        LOG.info("[飞禽走兽]创建房间[{}][{}]", roomDom.getId(), roomDom.getName());
      }
    }
  }

  /**
   * 创建房间 .
   * 
   * @return
   */
  private void createRooms(SharkRoomDic roomDic) {
    List<SharkRoomDomain> roomDomains = roomDic.list();
    if (EmptyUtil.listIsNotEmpty(roomDomains)) {
      for (SharkRoomDomain roomDom : roomDic.list()) {
        createRoom(roomDom);
      }
    }
  }

  /**
   * 重新加载房间，并返回最新房间id列表.
   *
   * @author abin
   * @date 2018年5月16日 下午2:48:38
   * @return 房间id列表.
   */
  public List<Integer> reloadRoom() {
    roomDic.load();
    List<Integer> ids = new ArrayList<>();
    GamesDomain gamesDom = gamesDic.get(Game.FQZS.getType());
    if (gamesDom != null && gamesDom.getActive()) {
      List<SharkRoomDomain> rooms = roomDic.list();
      if (EmptyUtil.listIsNotEmpty(rooms)) {
        for (SharkRoomDomain roomDomain : rooms) {
          if (roomDomain != null && roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
            ids.add(roomDomain.getId());
          }
        }
      }
    }
    return ids;
  }

  /**
   * 开启房间.
   *
   * @author abin
   * @date 2018年4月28日 下午2:14:26
   * @param roomId 房间id.
   * @return return old room(delete) list.
   */
  public List<SharkRoom> startRoom(int roomId, boolean reload) {
    if (reload) {
      roomDic.load();
    }

    List<SharkRoom> deleteRooms = new ArrayList<>();

    GamesDomain gamesDom = gamesDic.get(Game.FQZS.getType());
    if (gamesDom != null && gamesDom.getActive()) {
      SharkRoomDomain roomDomain = roomDic.get(roomId);

      if (roomDomain != null && roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
        SharkBaseRoom baseRoom = roomMap.get(roomId);

        if (baseRoom == null) {
          createRoom(roomDomain);
        } else {
          deleteRooms = baseRoom.renewRoom(roomDomain);
        }
      }
    }
    return deleteRooms;
  }

  /**
   * 获取最新房间实例.
   * 
   * @param roomId 房间id.
   * @return 21点房间.
   */
  public SharkRoom getNewestRoom(int roomId) {
    SharkRoom room = null;
    SharkBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      room = baseRoom.getNewInstance();
    }
    return room;
  }


  /**
   * 获取所有实例 .
   *
   * @author abin
   * @date 2018年4月28日 下午4:32:06
   * @param roomId 房间id.
   * @return 获取所有实例.
   */
  public List<SharkRoom> deleteRoom(int roomId) {
    roomDic.load();
    List<SharkRoom> rooms = null;
    SharkBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      rooms = new ArrayList<>(baseRoom.deleteAllInstance());
    }
    return rooms;
  }

  /**
   * 实例id获取房间实例 .
   *
   * @author abin
   * @date 2018年4月27日 下午6:01:41
   * @param instanceId 实例id.
   * @return 21点房间.
   */
  public SharkRoom getRoomByIntanceId(int instanceId) {
    SharkRoom room = null;
    int roomId = getRoomIdByIntanceId(instanceId);
    SharkBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      room = baseRoom.getByInstanceId(instanceId);
    }
    return room;
  }


  /**
   * 校验是否remove room.
   *
   * @author abin
   * @date 2018年4月28日 下午7:39:33
   * @param baseRoom 基本房间.
   */
  public void checkRemoveRoom(SharkBaseRoom baseRoom) {
    if (baseRoom.allInstance() == null || baseRoom.allInstance().isEmpty()) {
      roomMap.remove(baseRoom.getRoomId());
      baseRoom = null;
    }

  }

  /**
   * 清空房间实例.
   *
   * @author abin
   * @date 2018年4月28日 下午5:55:32
   * @param roomId 房间id.
   * @param instanceId 实例id.
   */
  public boolean checkRemoveInstance(int roomId, int instanceId) {
    boolean remove = false;
    SharkBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      SharkRoom room = baseRoom.getByInstanceId(instanceId);
      if (room == null) {
        System.out.println("...[" + instanceId + "] null...怎么可能");
      }
      Collection<SharkTable> tables = room.tables();

      if (tables == null || tables.isEmpty()) {
        baseRoom.removeInstance(instanceId);
        checkRemoveRoom(baseRoom);
        remove = true;
      }
    }
    return remove;
  }

  /**
   * 获取所有最新的房间.
   * 
   * @return 21点房间列表.
   */
  public Collection<SharkRoom> newestRooms() {
    List<SharkRoom> tempRooms = new ArrayList<>();
    for (SharkBaseRoom baseRoom : roomMap.values()) {
      SharkRoom sharkRoom = baseRoom.getNewInstance();
      if (sharkRoom != null && sharkRoom.getInstanceState().equals(InstanceState.NORMAL)) {
        tempRooms.add(sharkRoom);
      }
    }
    return tempRooms;
  }

  /**
   * 获取所有的房间列表 .
   *
   * @author abin
   * @date 2018年4月27日 下午4:22:40
   * @return 所有的房间列表.
   */
  public Collection<SharkRoom> allRooms() {
    List<SharkRoom> tempRooms = new ArrayList<>();
    for (SharkBaseRoom baseRoom : roomMap.values()) {
      Collection<SharkRoom> allInstances = baseRoom.allInstance();
      if (allInstances != null && !allInstances.isEmpty()) {
        tempRooms.addAll(allInstances);
      }
    }
    return tempRooms;
  }

  public Collection<SharkBaseRoom> allBaseRoom() {
    return roomMap.values();
  }



  /**
   * 获取牌桌.
   * 
   * @param tableId 桌子id.
   * @return 21点桌子.
   */
  public SharkTable getTable(int tableId) {
    SharkTable table = null;
    int roomId = getRoomIdByTableId(tableId);
    SharkBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      int instanceId = getInstanceIdByTableId(tableId);
      SharkRoom room = baseRoom.getByInstanceId(instanceId);
      if (room != null) {
        table = room.getTables().get(tableId);
      }
    }
    return table;
  }

  /**
   * 根据桌子id计算房间实例id.
   * 
   * @param tableId 桌子Id.
   * @return 房间Id.
   */
  public int getInstanceIdByTableId(int tableId) {
    return tableId / SharkTable.MAX_TABLE;
  }

  /**
   * 根据实例id计算房间id .
   *
   * @author abin
   * @date 2018年4月27日 下午5:54:05
   * @param instanceId 实例id.
   * @return 房间id.
   */
  public int getRoomIdByIntanceId(int instanceId) {
    return instanceId / SharkRoom.MAX_INSTANCE;
  }

  /**
   * 根据桌子获取房间id .
   *
   * @author abin
   * @date 2018年4月27日 下午5:56:03
   * @param tableId 桌子id.
   * @return 房间id.
   */
  public int getRoomIdByTableId(int tableId) {
    return getRoomIdByIntanceId(getInstanceIdByTableId(tableId));
  }

  /**
   * 获取座位 .
   * 
   * @param playerId 玩家id.
   * @return
   */
  public SharkSeat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }

  /**
   * 更新玩家座位 .
   * 
   * @param playerId 玩家id.
   * @param seat 座位信息.
   */
  public void updatePlayerSeats(long playerId, SharkSeat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家seat .
   * 
   * @param playerId 玩家id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }


}
